// import { createErrorMessage, ErrorMessage } from "@packages/shared";
import mongoose from "mongoose";
import { registerResource, ResourceAction } from "./core";
import { userEntrySchema, userActionValid } from "@packages/shared/validator/user.valid";
// import { generateRawSchema } from "zod-to-mongoose";
import { createErrorMessage, ErrorMessage } from "@packages/shared";

// 直接使用 mongoose.Schema 创建 schema
const userSchema = new mongoose.Schema({
    username: {
        type: String,
        required: true,
        unique: [true, createErrorMessage(ErrorMessage.Unique, '用户名')],
        minlength: 3,
        maxlength: 20,
    },
    password: {
        type: String,
        select: false,
        minlength: 6,
        maxlength: 20,
    },
    age: {
        type: Number,
        min: 0,
        max: 150,
    },
    gender: {
        type: String,
        enum: ['男', '女'],
    },
    nickName: {
        type: String,
        minlength: 3,
        maxlength: 20,
    },
    phone: {
        type: String,
        required: true,
        unique: [true, createErrorMessage(ErrorMessage.Unique, '手机号')],
    }
})

// 使用 zod-to-mongoose 生成 mongoose schema
// const userSchema = new mongoose.Schema(generateRawSchema({ schema: userEntrySchema }))

// 创建unique索引
// userSchema.index({
//     username: 1,
//     phone: 1,
// }, { unique: true })

// 创建一般索引
// userSchema.index({
//     username: 1,
//     phone: 1,
//     nickName: 1
// })

const User = mongoose.model('User', userSchema)

export default User

registerResource({
    model: User,
    description: '用户',
    action: [...ResourceAction.ALL],
    validator: userActionValid
})